package com.hp.ssmp.controller;

import com.hp.ssmp.entity.TOrderDetails;
import com.hp.ssmp.service.DetailsService;
import com.hp.ssmp.util.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/details")
public class DetailsController {

    @Resource
    private DetailsService detailsService;

    /**
     * 根据订单id查询详情
     * @param orderid
     * @return
     */
    @GetMapping("/listByOrder")
    public R<List<TOrderDetails>> listByOrder(Long orderid) {
        List<TOrderDetails> detailsList = detailsService.getDetailsList(orderid);
        return R.success(detailsList);
    }

    @GetMapping("/getById")
    public R<TOrderDetails> getById(Long detailsId) {
        TOrderDetails details = detailsService.getById(detailsId);
        return R.success(details);
    }

    @PostMapping("/add")
    public R add(TOrderDetails tOrderDetails) {
        boolean result = detailsService.save(tOrderDetails);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @GetMapping("/del")
    public R del(Long detailsId) {
        boolean result = detailsService.removeById(detailsId);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @PostMapping("/update")
    public R update(TOrderDetails tOrderDetails) {
        boolean result = detailsService.updateById(tOrderDetails);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }


}
